﻿using System;
using Ausy.Silverlight.Survey.AccountModule.Model;
using Ausy.Silverlight.Survey.AccountModule.Services.ServiceEventArgs;
using Reference = Ausy.Silverlight.Survey.AccountModule.AccountServiceReference;

namespace Ausy.Silverlight.Survey.AccountModule.Services
{
    /// <summary>
    /// Cette classe définit le service de gestion des compte utilisateur
    /// </summary>
    public class AccountService : IAccountService
    {
        #region Private Members

        /// <summary>
        /// Client du Proxy de gestion de compte
        /// </summary>
        private Reference.AccountServiceClient serviceClient;

        #endregion

        #region public CreateAccountCompleted Event

        /// <summary>
        /// Evènement lorsque la création du compte est terminée
        /// </summary>
        public event EventHandler<AccountEventArgs> CreateAccountCompleted;

        /// <summary>
        /// Evènement lorsque la création du compte est terminée
        /// </summary>
        private void OnCreateAccountCompleted(AccountEventArgs e)
        {
            EventHandler<AccountEventArgs> handler = CreateAccountCompleted;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        #endregion

        #region CreateAccountAsync Method

        /// <summary>
        /// Cette méyhode permet de créer un nouveau compte utilisateur
        /// </summary>
        /// <param name="account">Compte utilisateur</param>
        public void CreateAccountAsync(Account account)
        {
            this.serviceClient = new Reference.AccountServiceClient("AccountServiceEndPoint");
            this.serviceClient.CreateAccountCompleted += new EventHandler<Reference.CreateAccountCompletedEventArgs>(clientCreateAccountCompleted);

            var referenceAccount = new Reference.Account()
            {
                Identifier = account.Identifier,
                FirstName = account.FirstName,
                LastName = account.LastName,
                Email = account.Email,
                Phone = account.Phone,
                CompanyName = account.CompanyName,
                Experience = account.Experience,
                Function = account.Function
            };
            this.serviceClient.CreateAccountAsync(referenceAccount, account);

            this.OnCreateAccountCompleted(new AccountEventArgs(account, false));
        }

        /// <summary>
        /// Cette méthode permet de traiter le retour du service pour récupére la validation du compte crée
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void clientCreateAccountCompleted(object sender, Reference.CreateAccountCompletedEventArgs e)
        {
            this.serviceClient.CreateAccountCompleted -= this.clientCreateAccountCompleted;
            this.serviceClient.CloseAsync();

            var result = true;
            var account = new Account();
            if (e.Error == null)
            {
                result = !e.Result;
                account = e.UserState as Account;
            }
            this.OnCreateAccountCompleted(new AccountEventArgs(account, result));
        }

        #endregion
    }
}
